#!/bin/bash

# Unmount all registered repositories before unloading
# the Kext.
GVFSBINPATH="/usr/local/vfsforgit/gvfs"
if [ -f "${GVFSBINPATH}" ]; then
    unmountCmd="${GVFSBINPATH} service --unmount-all"
    echo $unmountCmd
    eval $unmountCmd || exit 1
fi

KEXTBUNDLEID="org.vfsforgit.PrjFSKext"
isKextLoadedCmd="/usr/sbin/kextstat -l -b $KEXTBUNDLEID | wc -l"
isKextLoaded=$(eval $isKextLoadedCmd)
if [ "$isKextLoaded" -gt 0 ]; then
    unloadCmd="/sbin/kextunload -b $KEXTBUNDLEID"
    echo $unloadCmd
    eval $unloadCmd || exit 1
fi

LEGACYKEXTBUNDLEID="io.gvfs.PrjFSKext"
isKextLoadedCmd="/usr/sbin/kextstat -l -b $LEGACYKEXTBUNDLEID | wc -l"
isKextLoaded=$(eval $isKextLoadedCmd)
if [ "$isKextLoaded" -gt 0 ]; then
    unloadCmd="/sbin/kextunload -b $LEGACYKEXTBUNDLEID"
    echo $unloadCmd
    eval $unloadCmd || exit 1
fi
